import axios from 'axios'
import qs from 'qs'
import baseUrl from '../../util/url.js'
import session from "../../util/session"
const url = baseUrl + 'login.php'
const url2 = baseUrl + 'api.php/records/users'
export default {
    namespaced: true,
    state: {
      loading: false,
      list: [],
      namearr:[],
    },
    mutations: {},
    actions: {
      doReg({ state, commit, dispatch }, user) {
        return new Promise((resolve, reject) => {
          axios({
            method: 'post',
            url: url2,
            data: user
          }).then(res => {
            resolve()
          }).catch(e => {
            reject()
          })
        })
      },
      doLogin({ state, commit, dispatch }, user) {
        return new Promise((resolve, reject) => {
          axios({
            method: 'post',
            // headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            url,
            data: qs.stringify({name:user.loginName,key:user.loginKey})
          }).then(res => {
            if (res.data.success) {
              // res.data.data.token = res.data.token
              session.save(res.data.data)
              resolve()
            } else {
              reject(res.data.message)
            }
          }).catch(e => {
            reject()
          })
        });
      }
    },
    getters: {}
  }